// import CryptoJS from 'crypto-js'
// @ts-ignore
import nprogress from "nprogress";

// 浏览器显示全屏动画
export function showNprogress() {
  nprogress.start();
}

// 浏览器隐藏全屏动画
export function hiddenNprogress() {
  nprogress.done();
}

/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params: any) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== "" &&
            typeof value[key] !== "undefined"
          ) {
            let params = propName + "[" + key + "]";
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}

// DES 加密 key与后端一样的秘钥(8的倍数)   message(值)
// export function encryptByDES(message, key = "7396101173961011") {
//   var keyHex = CryptoJS.enc.Utf8.parse(key);
//   var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
//       mode: CryptoJS.mode.ECB,
//       padding: CryptoJS.pad.Pkcs7
//   });
//   return encrypted.toString();
// }

// DES 解密
// export function decryptByDES(ciphertext, key = "7396101173961011") {
//   var keyHex = CryptoJS.enc.Utf8.parse(key);
//   // direct decrypt ciphertext
//   var decrypted = CryptoJS.DES.decrypt({
//       ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
//   }, keyHex, {
//       mode: CryptoJS.mode.ECB,
//       padding: CryptoJS.pad.Pkcs7
//   });
//   return decrypted.toString(CryptoJS.enc.Utf8);
// }

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(
  data: any,
  id?: any,
  parentId?: any,
  children?: any,
) {
  let config = {
    id: id || "id",
    parentId: parentId || "parentId",
    childrenList: children || "children",
  };

  var childrenListMap: any = {};
  var nodeIds: any = {};
  var tree: any = [];

  for (let d of data) {
    let parentId = d[config.parentId];
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = [];
    }
    nodeIds[d[config.id]] = d;
    childrenListMap[parentId].push(d);
  }

  for (let d of data) {
    let parentId = d[config.parentId];
    if (nodeIds[parentId] == null) {
      tree.push(d);
    }
  }

  for (let t of tree) {
    adaptToChildrenList(t);
  }

  function adaptToChildrenList(o: any) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]];
    }
    if (o[config.childrenList]) {
      for (let c of o[config.childrenList]) {
        adaptToChildrenList(c);
      }
    }
  }
  return tree;
}

/**
 * 把日期范围构造成beginTime AND endTime
 * @param params 传递对象参数
 * @param dateRange 日期数组
 * @param propName1 自定义名称1
 * @param propName1 自定义名称2
 * @returns
 */
export function koiDatePicker(
  searchParams: object,
  dateRange: any,
  propName1?: string,
  propName2?: string,
) {
  searchParams =
    typeof searchParams === "object" &&
    searchParams !== null &&
    !Array.isArray(searchParams)
      ? searchParams
      : {};
  dateRange = Array.isArray(dateRange) ? dateRange : [];
  if (
    propName1 != null &&
    propName1 != "" &&
    propName2 != null &&
    propName2 != ""
  ) {
    // 创建一个空的对象
    const dataParams: any = {};
    dataParams[propName1] = dateRange[0];
    dataParams[propName2] = dateRange[1];
    return Object.assign({}, searchParams, dataParams);
  } else {
    const dataParams = {
      beginTime: dateRange[0],
      endTime: dateRange[1],
    };
    return Object.assign({}, searchParams, dataParams);
  }
}

/**
 * 回显数据字典，进行状态翻译(数组对象)
 * @param datas 当前状态数据列表
 * @param value 需要进行翻译的值
 * @returns
 */
export function selectDictLabel(datas: any, value: any) {
  if (value === undefined || value === null || value === "") {
    return "";
  }
  var actions = [];
  Object.keys(datas).map((key) => {
    // 循环数据的获取key
    if (datas[key].dictValue === "" + value) {
      actions.push(datas[key].dictLabel);
      return true;
    }
  });
  if (actions.length === 0) {
    actions.push(value);
  }
  return actions.join("");
}
